package com.four.bergain.config;

import com.four.bergain.dao.BargainUserMapper;
import com.four.bergain.entity.BargainUser;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
/**
 * 2021/12/19
 * @author hxw
 */
@Configuration
public class RabbitMqConfig {

    @Autowired
    private BargainUserMapper userMapper;

    @RabbitListener(queues = "order-cunsume")
    @RabbitHandler
    public void msg(Message message, BargainUser bUser, Channel channel) {

        try {
            BargainUser byId = userMapper.getById(bUser.getUserId(), bUser.getBarId());
            if (byId!=null && byId.getBarStatus()!=1) {
                userMapper.updateBargainUser(2,byId.getBarUid());
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
